package com.example.springboot01.dao.impl;

import com.example.springboot01.dao.StudentDao;
import com.example.springboot01.pojo.Student;
import com.example.springboot01.utils.DBUtils;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;

@Repository
public class StudentDaoimpl implements StudentDao {
    @Override
    public void Save(Student student) {
        if (1== DBUtils.Save("insert into tb_student(stu_name,stu_gender,stu_birthday,stu_username,stu_password) values(?,?,?,?,?)",new Object[]{student.getStuName(),student.getStuGender(),student.getStuBrithday(),student.getStuUsername(),student.getStuPassword()})){
            System.out.println("保存成功");
        }else System.out.println("保存失败");
    }

    @Override
    public void DeleteById(Integer id) {
        if (id==null){
            System.out.println("参数不能为空");
            return ;
        }
        if (1<= DBUtils.DeleteById("delete from tb_student where id = ? ",id)){
            System.out.println("删除ID为："+id+" 的操作成功");
        }else System.out.println("删除ID为："+id+" 的操作失败");
    }

    @Override
    public void UpdateById(Student student) {
        if (student==null){
            System.out.println("参数不能为空");
            return ;
        }
        if (1<= DBUtils.Update("update tb_student set stu_name = ?,stu_gender = ?,stu_birthday = ?,stu_username = ?,stu_password = ? where id = ?",new Object[]{student.getStuName(),student.getStuGender(),student.getStuBrithday(),student.getStuUsername(),student.getStuPassword(),student.getId()})){
            System.out.println("更新ID为："+student.getId()+" 的操作成功");
        }else System.out.println("更新ID为："+student.getId()+" 的操作失败");
    }

    @Override
    public Student getStudentById(Integer id) {
        if (id==null){
            System.out.println("参数不能为空");
            return null;
        }
        String sql="select * from tb_student where id = ?";
        return DBUtils.getById(sql,id);
    }

    @Override
    public ArrayList<Student> getAll() {
        return DBUtils.getAll("select * from tb_student");
    }
}
